package org.kms.Controllers;

import org.kms.dao.xqwhDao;
import org.kms.pojo.Xqwh;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Xqwh")
@CrossOrigin(value = "*")
public class xqwh {
    @Autowired
    private xqwhDao xqwhDao;

    @RequestMapping("/index")
    @ResponseBody
    public ResponseResult<List<Xqwh>> getall() throws Exception{
        return new ResponseResult<>(xqwhDao.getAll());
    }

    @RequestMapping("/xqwhAdd")
    @ResponseBody
    public ResponseResult xqwhAdd(Xqwh xqwh) throws Exception{
        xqwhDao.add(xqwh);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/xqwhDelete")
    @ResponseBody
    public ResponseResult xqwhDelete(Integer tMid) throws Exception{
        xqwhDao.delete(tMid);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/xqwhUpdate")
    @ResponseBody
    public ResponseResult xqwhUpdate(Xqwh xqwh) throws Exception{
        xqwhDao.update(xqwh);
        return ResponseResult.createSuccessResult();
    }

    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }
}
